package com.zkdcloud.repository;

import com.zkdcloud.domain.User;
import org.jfaster.mango.annotation.DB;
import org.jfaster.mango.annotation.SQL;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户repository(包括)
 *
 * @author zk123
 * @version 2018/4/29
 */
@DB(table = "user")
@Repository
public interface UserDao {
    String COLUMN = " id,username,nickname,password,mobile,email,avatar_url,last_address_id,create_time ";

    /**
     * 新增user
     *
     * @param user user
     */
    @SQL("insert into #table ("+COLUMN+") values(:id,:username,:nickname,:password,:mobile,:email,:avatarUrl,:lastAddressId,:createTime)")
    void add(User user);

    /**
     * 根据username and password 筛选
     *
     * @param username username
     * @param password pwd
     * @return user
     */
    @SQL("select "+COLUMN+" from #table where username = :1 and password = :2")
    List<User> getByUsernameAndPwd(String username, String password);

    @SQL("select " + COLUMN + " from #table where mobile = :1")
    List<User> getByPhotoNum(String photoNumber);

    @SQL("select " + COLUMN + " from #table where mobile = :1 and password = :2")
    List<User> getByPhotoNumAndPwd(String photoNumber,String password);
    /**
     * 更新user
     *
     * @param user user
     */
    @SQL("update #table set id = :id " +
            " #if(:nickname != null && :nickname != '') ,nickname = :nickname  #end " +
            " #if(:avatarUrl != null && :avatarUrl != '') ,avatar_url = :avatarUrl  #end " +
            " #if(:lastAddressId != null && :lastAddressId != '') ,last_address_id = :lastAddressId  #end " +
            " #if(:mobile != null && :mobile != '') ,mobile = :mobile  #end " +
            " where id = :id")
    void updateUser(User user);

    /**
     * 根据id获取user
     *
     * @param id id
     * @return user
     */
    @SQL("select " + COLUMN + " from #table where id = :1")
    User getById(String id);
}
